<template>
	<div class="w-full h-full">
		<div id="video-container"></div>
	</div>
</template>
<script setup lang="ts">
// @ts-ignore
import EZUIKit from 'ezuikit-js';
import axios from 'axios';
import { useMessage } from '/@/hooks/message';
const EZUIKitRef: any = ref();
const emits = defineEmits(['close']);
async function initVideo(params: { appKey: string; appSecret: string; deviceSerial: string }) {
	try {
		// @ts-ignore
		// 获取toekn
		const { data: tokenData } = await axios.post(
			'https://open.ys7.com/api/lapp/token/get',
			{ appKey: params.appKey, appSecret: params.appSecret },
			{
				headers: {
					'Content-Type': 'application/x-www-form-urlencoded',
				},
			}
		);
		if (tokenData.code !== '200') {
			useMessage().error(tokenData.msg);
			emits('close');
			return;
		}
		console.log('tokenData: ', tokenData);
		// 获取直播地址
		const { data: addressData } = await axios.post(
			'https://open.ys7.com/api/lapp/v2/live/address/get',
			{
				accessToken: tokenData.data.accessToken,
				deviceSerial: params.deviceSerial,
			},
			{
				headers: {
					'Content-Type': 'application/x-www-form-urlencoded',
				},
			}
		);
		if (addressData.code !== '200') {
			useMessage().error(addressData.msg);
			emits('close');
			return;
		}
		console.log('accessToken: ', addressData.data.accessToken);
		EZUIKitRef.value = new (EZUIKit as any).EZUIKitPlayer({
			id: 'video-container', // 视频容器ID
			accessToken: tokenData.data.accessToken,
			url: addressData.data.url,
			// simple - 极简版; pcLive-pc直播；pcRec-pc回放；mobileLive-移动端直播；mobileRec-移动端回放;security - 安防版;voice-语音版;
			template: 'pcLive',
			// plugin: ['talk'], // 加载插件，talk-对讲
			width: 600,
			height: 440,
		});
		(window as any).EZUIKitRef = EZUIKitRef.value;
	} catch (err) {
		useMessage().wraning(`${err}`);
	}
}
const destroyPlayer = () => {
	if (EZUIKitRef.value) {
		unref(EZUIKitRef)?.stop();
		unref(EZUIKitRef)?.destroy();
	}
};
defineExpose({ initVideo, destroyPlayer });
</script>

<style lang="scss" scoped></style>
